import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/number/numberLog'

const api = {

    /**
     * 新增编号规则日志
     * @param numberLog
     * @returns {Promise<*|undefined>}
     */
    createNumberLog: (numberLog) =>
      axiosClient().post(`${requestUrl}/addInfo`, numberLog),

    /**
     * 根据ID删除编号规则日志
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteNumberLogById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新编号规则日志
     * @param id
     * @param numberLog
     * @returns {Promise<*|undefined>}
     */
    updateNumberLogFull: (id, numberLog) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, numberLog),

    /**
     * 根据ID部分更新编号规则日志
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateNumberLogPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询编号规则日志
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getNumberLogById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询编号规则日志
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getNumberLogPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询编号规则日志
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getNumberLogList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.ruleId) {
            params.append('ruleId', businessParam.ruleId)
        }
        if (businessParam.generatedNum) {
            params.append('generatedNum', businessParam.generatedNum)
        }
        if (businessParam.bizType) {
            params.append('bizType', businessParam.bizType)
        }
        if (businessParam.bizId) {
            params.append('bizId', businessParam.bizId)
        }
        if (businessParam.bizTableName) {
            params.append('bizTableName', businessParam.bizTableName)
        }
        if (businessParam.generateTime) {
            params.append('generateTime', businessParam.generateTime)
        }
        if (businessParam.operatorId) {
            params.append('operatorId', businessParam.operatorId)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
